package com.dkd.handler;

import cn.elegent.ac.ACHandler;
import cn.elegent.ac.annotation.Topic;
import com.dkd.config.TopicConfig;
import com.dkd.config.constant.OrderStatus;
import com.dkd.contract.VendoutResultContract;
import com.dkd.entity.OrderEntity;
import com.dkd.service.OrderService;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author DiXiaoNie
 * @since 2023/2/5 12:28
 */
@Topic(TopicConfig.VMS_RESULT_TOPIC)
@Slf4j
public class VendoutResultMsgHandler implements ACHandler<VendoutResultContract> {

    @Autowired
    private OrderService orderService;


    @Override
    public void process(String topic, VendoutResultContract vendoutResultContract) throws Exception {
        log.info("执行出货结果回调：{}", vendoutResultContract);
        if (Strings.isNullOrEmpty(vendoutResultContract.getInnerCode())) {
            return;
        }
        //取出订单号
        String orderNo = vendoutResultContract.getVendoutData().getOrderNo();
        OrderEntity orderEntity = this.orderService.getByOrderNo(orderNo);
        //处理出货逻辑
        //如果出货成功
        if (vendoutResultContract.isSuccess()) {
            //更新订单状态
            orderEntity.setStatus(OrderStatus.ORDER_STATUS_VENDOUT_SUCCESS);
            log.info("出货成功 订单号：{}", orderNo);
            this.orderService.updateById(orderEntity);
        }
    }
}
